﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KingSite.Purview;
using System.Web.Security;
using KingSite.Purview.Core.Repository;
using KingSite.Purview.Repository;
using System.Configuration;
using KingSite.Purview.Domain;

namespace KingSite.PurviewMenuManager {
    public partial class MainFrm : Form {
        public MainFrm() {
            InitializeComponent();
        }

        private string connstr = "MoleShop";
        TreeNode root = null;

        private void Form1_Load(object sender, EventArgs e) {
            SetRolesList();
            SetListView();
            AddRootNode();
        }

        private TreeNode AddRootNode() {
            root = new TreeNode("根节点");
            NavTreeMenu menu = new NavTreeMenu();
            menu.MenuId = 0;
            root.Tag = menu;
            treeView1.Nodes.Add(root);
            return root;
        }

        private void SetListView() {
            listView1.Clear();
            listView1.Columns.Add("菜单编号");
            listView1.Columns.Add("菜单项");
            listView1.Columns.Add("路径");

            IList<Sys_Menu> list = GetMenues();
            foreach (Sys_Menu m in list) {
                ListViewItem item = new ListViewItem();
                item.Text = m.Id.ToString();
                item.SubItems.Add(m.MenuName);
                item.SubItems.Add(m.PathLocation);
                listView1.Items.Add(item);
            }
        }

        private void SetRolesList() {
            comboBox1.DataSource = GetRoles();
            comboBox1.Text = "Id";
            comboBox1.ValueMember = "RoleName";
        }

        private IList<Sys_Role> GetRoles() {
            ISys_RoleRepository r = new Sys_RoleRepository(connstr, DBType.SqlServer);
            IList<Sys_Role> list = r.FindAll();

            return list;
        }

        private IList<Sys_Menu> GetMenues() {
            ISys_MenuRepository r = new Sys_MenuRepository(connstr, DBType.SqlServer);
            IList<Sys_Menu> list = r.FindAll();

            return list;
        }

        private void button1_Click(object sender, EventArgs e) {
            Sys_Role role = comboBox1.SelectedItem as Sys_Role;
            ISys_MenuInRoleRepository r = new Sys_MenuInRoleRepository(connstr, DBType.SqlServer);
            IList<NavTreeMenu> list = r.FindRoleMenues(string.Format("'{0}'", role.RoleName));
            SetTreeView(list);
        }

        private void SetTreeView(IList<NavTreeMenu> list) {
            treeView1.Nodes.Clear();
            TreeNode root = AddRootNode();
            var topMenu = list.Where(a => a.ParentMenuId.Equals(0));
            foreach (NavTreeMenu menu in topMenu) {
                TreeNode node = root.Nodes.Add(menu.MenuName);
                node.Tag = menu;
                var menues = list.Where(a => a.ParentMenuId.Equals(menu.MenuId));
                foreach (NavTreeMenu m in menues) {
                    TreeNode n = node.Nodes.Add(m.MenuName);
                    n.Tag = m;
                }
            }
            treeView1.ExpandAll();
        }

        private void button2_Click(object sender, EventArgs e) {
            List<Sys_MenuInRole> list = new List<Sys_MenuInRole>();
            foreach (TreeNode node in root.Nodes) {
                Sys_MenuInRole mir = new Sys_MenuInRole();
                mir.MenuId = ((NavTreeMenu)node.Tag).MenuId;
                mir.ParentMenuId = ((NavTreeMenu)node.Tag).ParentMenuId;
                Sys_Role role = comboBox1.SelectedItem as Sys_Role;
                mir.RoleId = role.Id;
                list.Add(mir);
                foreach (TreeNode n in node.Nodes) {
                    Sys_MenuInRole mir2 = new Sys_MenuInRole();
                    mir2.MenuId = ((NavTreeMenu)n.Tag).MenuId;
                    mir2.ParentMenuId = ((NavTreeMenu)n.Tag).ParentMenuId;
                    mir2.RoleId = role.Id;
                    list.Add(mir2);
                }
            }
            InsertMenuInRole(list);
            MessageBox.Show("保存成功");


        }

        private void InsertMenuInRole(List<Sys_MenuInRole> list) {
            Sys_Role role = comboBox1.SelectedItem as Sys_Role;
            ISys_MenuInRoleRepository r = new Sys_MenuInRoleRepository(connstr, DBType.SqlServer);
            r.DeleteByRoleId(role.Id);
            foreach (Sys_MenuInRole m in list) {
                r.Insert(m);
            }
        }



        private void listBox1_DragLeave(object sender, EventArgs e) {
            string strItem = e.ToString();
            //开始进行"Drag"操作
            DoDragDrop(strItem, DragDropEffects.Copy | DragDropEffects.Move);
        }

        private void treeView1_ItemDrag(object sender, ItemDragEventArgs e) {
            DoDragDrop(e.Item, DragDropEffects.Move);
        }

        private void textBox1_DragEnter(object sender, DragEventArgs e) {
            TreeNode tn;
            if (e.Data.GetDataPresent(typeof(TreeNode))) {
                tn = (TreeNode)(e.Data.GetData(typeof(TreeNode)));
                TreeNode tPn = tn.Parent;
                if (tPn.Text == "RootManager") {
                    e.Effect = DragDropEffects.Copy;
                }
                else {
                    e.Effect = DragDropEffects.None;
                }

            }
            else
                e.Effect = DragDropEffects.None;
        }


        private void listView1_ItemDrag(object sender, ItemDragEventArgs e) {
            DoDragDrop(e.Item, DragDropEffects.Copy);
        }

        private void treeView1_DragEnter(object sender, DragEventArgs e) {
            ListViewItem item;
            if (e.Data.GetDataPresent(typeof(ListViewItem))) {
                item = (ListViewItem)(e.Data.GetData(typeof(ListViewItem)));

                e.Effect = DragDropEffects.Copy;

            }
            else if (e.Data.GetDataPresent(typeof(TreeNode)))
                e.Effect = DragDropEffects.Move;            
            else
                e.Effect = DragDropEffects.None;
        }

        private void treeView1_DragDrop(object sender, DragEventArgs e) {
            Point pt;
            ListViewItem item;
            TreeNode myNode = null;
            pt = ((TreeView)(sender)).PointToClient(new Point(e.X, e.Y));
            if (e.Data.GetDataPresent(typeof(ListViewItem))) {
                item = (ListViewItem)(e.Data.GetData(typeof(ListViewItem)));
                //根据鼠标坐标确定要移动到的目标节点

                TreeNode targeNode;

                targeNode = treeView1.GetNodeAt(pt);

                //NavTreeMenu menu = new NavTreeMenu();
                //menu.MenuId = Convert.ToInt32(item.SubItems[0].Text);
                //targeNode.Tag = menu;

                AddNode(item, targeNode);
            }
            else if (e.Data.GetDataPresent(typeof(TreeNode))) {
                myNode = (TreeNode)(e.Data.GetData(typeof(TreeNode)));

                TreeNode DropNode = this.treeView1.GetNodeAt(pt);
                // 1.目标节点不是空。2.目标节点不是被拖拽接点的字节点。3.目标节点不是被拖拽节点本身
                if (DropNode != null && DropNode.Parent != myNode && DropNode != myNode) {
                    TreeNode DragNode = myNode;
                    // 将被拖拽节点从原来位置删除。
                    myNode.Remove();
                    // 在目标节点下增加被拖拽节点
                    DropNode.Nodes.Add(DragNode);
                }
                // 如果目标节点不存在，即拖拽的位置不存在节点，那么就将被拖拽节点放在根节点之下
                if (DropNode == null) {
                    TreeNode DragNode = myNode;
                    myNode.Remove();
                    treeView1.Nodes.Add(DragNode);
                }

            }
            else {
                MessageBox.Show("error");
            }
            


        }

        private void AddNode(ListViewItem item, TreeNode node) {
            NavTreeMenu menu = new NavTreeMenu();
            menu.MenuId = Convert.ToInt32(item.SubItems[0].Text);
            menu.MenuName = item.SubItems[1].Text;
            menu.ParentMenuId = GetParentMenuId(node);
            menu.PathLocation = item.SubItems[2].Text;
            TreeNode n = node.Nodes.Add(menu.MenuName);
            n.Tag = menu;

            treeView1.ExpandAll();

        }

        private int GetParentMenuId(TreeNode node) {
            return ((NavTreeMenu)node.Tag).MenuId;
        }

        private void treeView1_KeyDown(object sender, KeyEventArgs e) {
            if (e.KeyData == Keys.Delete) {
                TreeNode DeleteNode = this.treeView1.SelectedNode;
                if (DeleteNode != null) {
                    DeleteNode.Remove();
                }
            }
        }


    }
}
